package com.gsmr.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.gsmr.entity.BS_info;
import com.gsmr.entity.FrontProcessorInfo;
import com.gsmr.entity.PageBean;
import com.gsmr.service.BS_infoService;
import com.gsmr.service.FrontProcessorInfoService;
import com.gsmr.service.SysOperationRecordService;
import com.gsmr.util.JsonUtil;
import com.gsmr.util.PropertityUtil;
import com.gsmr.util.ResponseUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
@RequestMapping("/frontProcessorInfo")
public class FrontProcessorInfoController {

	@Resource
	private FrontProcessorInfoService frontProcessorInfoService;

	@Resource
	private SysOperationRecordService sysOperationRecordService;

	@Resource
	private BS_infoService bS_infoService;

	/**
	 * 分页条件查询FrontProcessorInfo
	 */
	@RequestMapping("/list")
	public void list(@RequestParam(value = "page", required = false, defaultValue = "1") String page, @RequestParam(value = "rows", required = false, defaultValue = "20") String rows,
		@RequestParam(value = "frontProcessorId", required = false) String frontProcessorId, @RequestParam(value = "routeName", required = false) String routeName, FrontProcessorInfo frontProcessorInfo, HttpServletResponse response) throws Exception {

		PageBean pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(rows));
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("start", pageBean.getStart());
		map.put("size", pageBean.getPageSize());
		map.put("database_name", PropertityUtil.NETWORK_MANAGEMENT_DATABASE_NAME);
		map.put("frontProcessorId", frontProcessorId);
		// if(routeName!=null&&!routeName.equals("")){
		// List<String> strList = findFrontProcessorIds(routeName);
		// if(strList.size()<=0){
		// JSONObject result = new JSONObject();
		// String[] strArray = {};
		// result.put("rows", strArray);
		// result.put("total", 0);
		// ResponseUtil.write(response, result);
		//
		// }else{
		// map.put("FrontProcessorIds", strList);
		// }
		// }
		//long begin1 = System.currentTimeMillis();
		List<FrontProcessorInfo> frontprocessorInfoList = frontProcessorInfoService.find(map);
		//long end1 = System.currentTimeMillis();
		//long time = end1 - begin1;
		//System.out.println("搜索前置机信息时间：" + time);

		Long total = frontProcessorInfoService.getTotal(map);
		//long end2 = System.currentTimeMillis();
		//long time2 = end2 - end1;
		//System.out.println("total time:" + time2);
		JSONObject result = new JSONObject();
		// JSONArray jsonArray =
		// JSONArray.fromObject(frontprocessorInfoList,JsonUtil.jsonArrayNameFilter("frontProcessorStates"));
		JSONArray jsonArray = JSONArray.fromObject(frontprocessorInfoList);
		// System.out.println(frontprocessorInfoList);
		result.put("rows", jsonArray);
		result.put("total", total);
		ResponseUtil.write(response, result);
	}

	@RequestMapping("/topInfo")
	public void topInfo(HttpServletResponse response) throws Exception {
		List<FrontProcessorInfo> frontprocessorInfoList = frontProcessorInfoService.findFrontProcessorIdAndSiteName(null);
		for (int i = 0; i < frontprocessorInfoList.size(); i++) {
			frontprocessorInfoList.get(i).setFrontProcessorStates(null);
		}
		JSONObject result = new JSONObject();
		JSONArray jsonArray = JSONArray.fromObject(frontprocessorInfoList);
		// System.out.println(frontprocessorInfoList);
		result.put("rows", jsonArray);
		ResponseUtil.write(response, result);
	}

	/**
	 * 添加或者修改FrontprocessorInfo
	 * @param flag 1 添加 2修改
	 */
	@RequestMapping("/save")
	public void save(FrontProcessorInfo frontprocessorInfo, HttpServletResponse response, Integer flag) throws Exception {
		int resultTotal = 0; // 操作的记录条数
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("database_name", PropertityUtil.NETWORK_MANAGEMENT_DATABASE_NAME);
		if (flag == 1) {
			ArrayList<Map<String, String>> frontprocessorList = new ArrayList<Map<String, String>>();
			LinkedHashMap hm = new LinkedHashMap();
			hm.put("frontProcessorId", frontprocessorInfo.getFrontProcessorId());
			hm.put("siteName", frontprocessorInfo.getSiteName());
			hm.put("ipAddress", frontprocessorInfo.getIpAddress());
			hm.put("bcchInfo", frontprocessorInfo.getBcchInfo());
			hm.put("iconPositionX", frontprocessorInfo.getIconPositionX());
			hm.put("iconPositionY", frontprocessorInfo.getIconPositionY());
			hm.put("createTime", System.currentTimeMillis());
			hm.put("gprsSendSwitch", 2);
			hm.put("gprsReportedIpAddress", "0.0.0.0");
			frontprocessorList.add(hm);
			map.put("list", frontprocessorList);
			resultTotal = frontProcessorInfoService.add_batch(map);

			ArrayList<Map<String, String>> sysoperationrecordList = new ArrayList<Map<String, String>>();
			hm.clear();
			hm.put("operateTime", System.currentTimeMillis());
			hm.put("userId", 1);
			hm.put("operationType", 7);
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("frontProcessorId", frontprocessorInfo.getFrontProcessorId());
			hm.put("newValue", jsonObject.toString());
			hm.put("isImplemented", 0);
			sysoperationrecordList.add(hm);
			map.put("list", sysoperationrecordList);
			resultTotal = sysOperationRecordService.add(map);

		} else {
			map.put("frontProcessorId", frontprocessorInfo.getFrontProcessorId());
			map.put("ipAddress", frontprocessorInfo.getIpAddress());
			map.put("monitoringFrequency", frontprocessorInfo.getMonitoringFrequency());
			map.put("reportInterval", frontprocessorInfo.getReportInterval());
			map.put("spectrumSendSwitch", frontprocessorInfo.getSpectrumSendSwitch());
			map.put("spectrumReportedIpAddress", frontprocessorInfo.getSpectrumReportedIpAddress());
			map.put("signalSendSwitch", frontprocessorInfo.getSignalSendSwitch());
			map.put("signalReportedIpAddress", frontprocessorInfo.getSignalReportedIpAddress());
			map.put("tchSendSwitch", frontprocessorInfo.getTchSendSwitch());
			map.put("tchReportedIpAddress", frontprocessorInfo.getTchReportedIpAddress());
			map.put("gprsSendSwitch", frontprocessorInfo.getGprsSendSwitch());
			map.put("gprsReportedIpAddress", frontprocessorInfo.getGprsReportedIpAddress());
			resultTotal = frontProcessorInfoService.update(map);
		}
		JSONObject result = new JSONObject();
		if (resultTotal > 0) {
			result.put("success", true);
		} else {
			result.put("success", false);
		}
		ResponseUtil.write(response, result);

	}

	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	public void delete(@RequestParam(value = "ids") String ids, HttpServletResponse response) throws Exception {
		String[] idsStr = ids.split(",");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("database_name", PropertityUtil.NETWORK_MANAGEMENT_DATABASE_NAME);
		for (int i = 0; i < idsStr.length; i++) {
			map.put("ID", idsStr[i]);
			frontProcessorInfoService.delete(map);
		}
		JSONObject result = new JSONObject();
		result.put("success", true);
		ResponseUtil.write(response, result);

	}

	/**
	 * 判断是否存在指定前置机号
	 */
	@RequestMapping("/existFrontprocessorInfoName")
	public void existFrontprocessorInfoName(String frontProcessorId, HttpServletResponse response) throws Exception {
		JSONObject result = new JSONObject();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("database_name", PropertityUtil.NETWORK_MANAGEMENT_DATABASE_NAME);
		map.put("frontProcessorId", frontProcessorId);
		if (frontProcessorInfoService.findByFrontProcessorId(map) == null) {
			result.put("exist", false);
		} else {
			result.put("exist", true);
		}
		ResponseUtil.write(response, result);

	}

	/**
	 * 查找前置机号和区域信息
	 */
	@RequestMapping("/frontProcessorIdAndSiteNameComboList")
	public void frontProcessorIdAndSiteNameComboList(HttpServletResponse response) throws Exception {
		JSONArray jsonArray = new JSONArray();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("database_name", PropertityUtil.NETWORK_MANAGEMENT_DATABASE_NAME);
		List<FrontProcessorInfo> frontProcessorStateList = frontProcessorInfoService.findFrontProcessorIdAndSiteName(map);
		JSONArray rows = JSONArray.fromObject(frontProcessorStateList, JsonUtil.jsonArrayNameFilter("frontProcessorStates"));
		jsonArray.addAll(rows);
		ResponseUtil.write(response, jsonArray);

	}

	/**
	 * 查找铁路线路列表
	 */
	@RequestMapping("/routeNameComboList")
	public void routeNameComboList(HttpServletResponse response) throws Exception {
		JSONArray jsonArray = new JSONArray();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("database_name", PropertityUtil.GSMR_DATABASE);
		map.put("table_name", "bs_info");
		List<BS_info> bS_infoList = bS_infoService.findRouteNames(map);
		JSONArray rows = JSONArray.fromObject(bS_infoList);
		jsonArray.addAll(rows);
		ResponseUtil.write(response, jsonArray);
	}

	/**
	 * 查找FrontProcessorId
	 */
	public List<String> findFrontProcessorIds(String RouteName) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("database_name", PropertityUtil.GSMR_DATABASE);
		map.put("table_name", "bs_info");
		map.put("RouteName", RouteName);
		return bS_infoService.findFrontProcessorIds(map);
	}

	/**
	 * 查找铁路线路列表
	 */
	@RequestMapping("/find")
	public void bsInfoFind(HttpServletResponse response) throws Exception {
		JSONArray jsonArray = new JSONArray();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("database_name", PropertityUtil.GSMR_DATABASE);
		map.put("table_name", "bs_info");
		List<BS_info> bS_infoList = bS_infoService.find(map);
		JSONArray rows = JSONArray.fromObject(bS_infoList);
		jsonArray.addAll(rows);
		ResponseUtil.write(response, jsonArray);
	}
}